#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;

namespace fio {
const int SZ = (1 << 23);
char ibuf[SZ], obuf[SZ];
int ips = SZ, ops;
int stk[50], top;
char gc() {
    if (ips == SZ)
        fread(ibuf, 1, SZ, stdin), ips = 0;
    return ibuf[ips++];
}
void flush() { fwrite(obuf, 1, ops, stdout), ops = 0; }
void pc(char c) {
    if (ops == SZ)
        flush();
    obuf[ops++] = c;
}
template <typename tp>
void rd(tp& x) {
    x = 0;
    bool f = 0;
    char c = gc();
    for (; (c < '0' || c > '9') && c != '-'; c = gc())
        ;
    if (c == '-')
        f = 1, c = gc();
    for (; c >= '0' && c <= '9'; c = gc()) x = x * 10 + c - '0';
    if (f)
        x = -x;
}
template <typename _tp>
void wrt(_tp x) {
    if (x < 0)
        pc('-'), x = -x;
    for (x ? 0 : stk[++top] = 0; x; x /= 10) stk[++top] = x % 10;
    while (top) pc(stk[top--] + '0');
}
struct reader {
    template <typename tp>
    reader& operator>>(tp& x) {
        rd(x);
        return *this;
    }
} fin;
struct writer {
    writer& operator<<(const char* c) {
        for (int i = 0; c[i] != '\0'; i++) pc(c[i]);
        return *this;
    }
    template <typename _tp>
    writer& operator<<(_tp x) {
        wrt(x);
        return *this;
    }
} fout;
}  // namespace fio
int mdep[N];
int son[N],son1[N];
int siz[N];
vector<int> bian[N];
int c[N];
int dep[N];
int fath[N];
void dfs(int x,int fa){
    mdep[x] = 1;
    son[x] = 0;
    dep[x] = dep[fa] + 1;
    siz[x] = 1;
    fath[x] = fa;
    for(int v: bian[x]){
        dfs(v,x);
        mdep[x] = max(mdep[x],mdep[v] + 1);
        if(mdep[v] > mdep[son[x]]){
            son[x] = v;
        }
        if(siz[v] > siz[son1[x]]){
            son1[x] = v;
        }
        siz[x] += siz[v];
    }
}
int top1[N];
void dfs4(int x,int tf){
    top1[x] = tf;
    if(son1[x]){
        dfs4(son[x],tf);
    }
    for(int v : bian[x]){
        if(v == son[x]){
            continue;
        }
        dfs4(v,v);
    }
}
int top[N];
int dfn[N],rnk[N];
int dft;
void dfs1(int x,int tf){
    dfn[x] = ++dft;
    rnk[dft] = x;
    top[x] = tf;
    if(son[x]){
        dfs1(son[x],tf);
    }
    for(int v: bian[x]){
        if(v == son[x]){
            continue;
        }
        dfs1(v,v);
    }
}
struct segment_tree{
    int ls[N*70],rs[N*70],pcnt;
    int sum[N*70];
    void update(int &now,int cop,int l,int r,int p,int v){
        now = ++pcnt;
        sum[now] = sum[cop] + v;
        ls[now] = ls[cop];
        rs[now] = rs[cop];
        if(l == r){
            return;
        }
        int mid = (l + r)/2;
        if(p <= mid){
            update(ls[now],ls[cop],l,mid,p,v);
        }else{
            update(rs[now],rs[cop],mid+1,r,p,v);
        }
    }
    int query(int now,int l,int r,int x,int y){
        if(x <= l && r <= y){
            return sum[now];
        }
        int mid = (l + r)/2;
        int su = 0;
        if(x <= mid){
            su += query(ls[now],l,mid,x,y);   
        }
        if(mid < y){
            su += query(rs[now],mid+1,r,x,y);
        }
        return su;
    }
    int query(int now,int l,int r,int p){
        if(l == r){
            return sum[now];
        }
        int mid = (l + r)/2;
        if(p <= mid){
            return query(ls[now],l,mid,p);
        }else{
            return query(rs[now],mid+1,r,p);
        }
    }
}sg;
int rot[N];
int lca(int u,int v){
    while(top[u] != top[v]){
        if(dep[top[u]] < dep[top[v]]){
            swap(u,v);
        }
        u = fath[top[u]];
    }
    if(dep[v] < dep[u]){
        return v;
    }else{
        return u;
    }
}
vector<int> cf[N];
bool imp[N];
int mad[N];
vector<pair<int,int>> opers[N];
vector<int> bian1[N];
void dfs3(int x){
    if(imp[x]){
        mad[x] = dep[x];
    }else{
        mad[x] = 0x3f3f3f3f;
    }
    for(int v : bian1[x]){
        dfs3(v);
        if(mad[v] < mad[x]){
            if(mad[x] != 0x3f3f3f3f){
                opers[x].push_back(make_pair(mad[x],-1));
            }
            mad[x] = mad[v];
        }else{
            opers[x].push_back(make_pair(mad[v],-1));
        }
    }
    if(imp[x]){
        opers[x].push_back(make_pair(mad[x],1));
    }
}
void dfs2(int x){
    if(son[x]){
        dfs2(son[x]);
        rot[x] = rot[son[x]];
    }else{
        rot[x] = 0;
    }
    for(int v :bian[x]){
        if(v == son[x]){
            continue;
        }
        dfs2(v);
        for(int i = dep[v];i<=dep[v] + mdep[v] - 1;i++){
            sg.update(rot[x],rot[x],1,mdep[1],i,sg.query(rot[v],1,mdep[1],i));
        }
    }
    for(pair<int,int> ops : opers[x]){
        sg.update(rot[x],rot[x],1,mdep[1],ops.first,ops.second);
    }
}
void solve(){
    int n,m;
    fio::fin>>n>>m;
    dft = 0;
    for(int i = 1;i<=n;i++){
        fio::fin>>c[i];
        bian[i].clear();
        cf[i].clear();
        opers[i].clear();
    }
    for(int i = 1;i<=n;i++){
        cf[c[i]].push_back(i);
    }
    for(int i = 2;i <= n;i++){
        int f;
        fio::fin>>f;
        bian[f].push_back(i);
    }
    dfs(1,0);
    dfs4(1,0);
    dfs1(1,1);
    for(int i = 1;i<=n;i++){
        if(!cf[i].size()){
            continue;
        }
        sort(cf[i].begin(),cf[i].end(),[](int va,int vb){
            return dfn[va] < dfn[vb]; 
        });
        vector<int> p(cf[i]);
        imp[p[0]] = 1;
        for(int j = 1;j < cf[i].size();j++){
            p.push_back(lca(p[j],p[j - 1]));
            imp[p[j]] = 1;
        }
        sort(p.begin(),p.end(),[](int va,int vb){
            return dfn[va] < dfn[vb]; 
        });
        p.erase(unique(p.begin(),p.end()),p.end());
        for(int j = 1;j < p.size();j++){
            int lc = lca(p[j],p[j - 1]);
            bian1[lc].push_back(p[j]);
        }
        dfs3(p[0]);
        for(int v: p){
            imp[v] = 0;
            mad[v] = 0x3f3f3f3f;
            bian1[v].clear();
        }
    }
    dfs2(1);
    int last = 0;
    for(int i = 1;i<=m;i++){
        int x,d;
        fio::fin>>x>>d;
        x^=last;
        d^=last;
        last = sg.query(rot[x],1,mdep[1],dep[x],min(dep[x] + d,mdep[1])); 
        fio::fout<<last;
        fio::fout<<"\n";
    }
}
int main(){
    int T;
    fio::fin>>T;
    while(T--){
        sg.pcnt = 0;
        solve();
        //cerr<<sg.pcnt<<'\n';
    }
    fio::flush();
}